#include "../include/bitmap.h"

void
initBitmap(bitmap *  bp, char * bpPtr, int size) {
	bp->bpPtr = bpPtr;
	bp->size = size;

	//seteo el bitmap de inodos para que no haya ninguno presente
	int i;
	for (i = 0; i < size; i++) {
		bp->bpPtr[i] = 0;
	}
}

void
turnBitOn(bitmap *  bp, int pos) {
	if (pos < 0)
		return;
	//prendo el bit que corresponda
	bp->bpPtr[pos/8] |= SET_BIT(pos);
}

void
turnBitOff(bitmap *  bp, int pos) {
	if (pos < 0)
		return;
	//apago el bit que corresponda
	bp->bpPtr[pos/8] &= ~SET_BIT(pos);
}

int
isBitOn(bitmap *  bp, int pos) {
	if (pos < 0)
		return -1;
	return bp->bpPtr[pos/8] & SET_BIT(pos);
}

int
findFreePos(bitmap *  bp, int pos) {
	int i, cant = bp->size * 8;
	i = (pos >= 0)? pos : 0;

	for (; i < cant; i++) {
		if (!isBitOn(bp, i)) {
			return i;
		}
	}
	return -1;
}
